package ui;

import handler.AnrufHandler;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.event.MouseInputListener;

import map.SelectionAdapter;
import map.SelectionPainter;

import org.jdesktop.swingx.JXMapViewer;
import org.jdesktop.swingx.OSMTileFactoryInfo;
import org.jdesktop.swingx.input.CenterMapListener;
import org.jdesktop.swingx.input.PanKeyListener;
import org.jdesktop.swingx.input.PanMouseInputListener;
import org.jdesktop.swingx.input.ZoomMouseWheelListenerCursor;
import org.jdesktop.swingx.mapviewer.DefaultTileFactory;
import org.jdesktop.swingx.mapviewer.GeoPosition;
import org.jdesktop.swingx.mapviewer.LocalResponseCache;
import org.jdesktop.swingx.mapviewer.TileFactoryInfo;

import datatypes.Anruf;

import ui.MarqueePanel;

public class MainGUI extends JFrame implements AnrufHandler{
	
	private static final long serialVersionUID = 5567439530076976438L;
	
	JXMapViewer map;
	MarqueePanel lauftext;
	
	public MainGUI()
	{
		Container contentPane = getContentPane();
		if(!(contentPane.getLayout() instanceof BorderLayout))
		{
			contentPane.setLayout(new BorderLayout());
		}
		
		lauftext = new MarqueePanel("Das ist ein toller text", 100);
		contentPane.add(lauftext,BorderLayout.PAGE_END);
		lauftext.start();
		
		
		map = createMap();
		
		map.setName("map");
		map.setPreferredSize(new Dimension(400, 400));
		contentPane.add(map,BorderLayout.LINE_START);
		
		map.setCenterPosition(new GeoPosition(51.428755, 6.772968));
		map.setZoom(7);
		
		JButton btn = new JButton("Add Text");
		btn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				lauftext.appendText("Neuer Text! ");
				
			}
		});
		
		contentPane.add(btn,BorderLayout.LINE_END);
		
		setSize(1024, 600);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}
	
	private JXMapViewer createMap()
	{
		// Create a TileFactoryInfo for OpenStreetMap
		TileFactoryInfo info = new OSMTileFactoryInfo();
		DefaultTileFactory tileFactory = new DefaultTileFactory(info);
		tileFactory.setThreadPoolSize(8);

		// Setup local file cache
		File cacheDir = new File(System.getProperty("user.home") + File.separator + ".jxmapviewer2");
		LocalResponseCache.installResponseCache(info.getBaseURL(), cacheDir, false);

		// Setup JXMapViewer
		JXMapViewer mapViewer = new JXMapViewer();
		mapViewer.setTileFactory(tileFactory);

		// Set the focus
		mapViewer.setZoom(7);
	
		// Add interactions
		MouseInputListener mia = new PanMouseInputListener(mapViewer);
		mapViewer.addMouseListener(mia);
		mapViewer.addMouseMotionListener(mia);

		mapViewer.addMouseListener(new CenterMapListener(mapViewer));
		
		mapViewer.addMouseWheelListener(new ZoomMouseWheelListenerCursor(mapViewer));
		
		mapViewer.addKeyListener(new PanKeyListener(mapViewer));
		
		// Add a selection painter
		SelectionAdapter sa = new SelectionAdapter(mapViewer); 
		SelectionPainter sp = new SelectionPainter(sa); 
		mapViewer.addMouseListener(sa); 
		mapViewer.addMouseMotionListener(sa); 
		mapViewer.setOverlayPainter(sp);
		
		return mapViewer;
	}

	@Override
	public void initAnruf(Anruf anruf) {

	}
	
	
}
